package zj.com.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.DbManager;

import zj.com.activities.DqrSggxActivity;
import zj.com.activities.GjgxActivity;
import zj.com.activities.StcjActivity;
import zj.com.activities.StcjListActivity;
import zj.com.activities.ZlzpActivity;
import zj.com.activities.ZlzpListActivity;
import zj.com.db.DBUtil;
import zj.com.model.Privilege;
import zj.com.net.Urls;
import zj.com.util.Constants;
import zj.com.util.ImageUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
//现场检查
public class ZljkFragment extends Fragment implements View.OnClickListener{

    public RelativeLayout gxys_rl,zlzp_rl,d_sggx_rl,stcj_rl;
    public TextView title_name,right_tv,p_zlzp_tv,p_stcj_tv;
    PopupWindow popupWindow;
    Intent intent;

    public RelativeLayout st_upload_rl;
    public ImageView banner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zljk, container, false);
        initView(view);
        return view;
    }

    public Privilege gjgx_select;
    public Privilege gjgx_confirm;
    public Privilege gjgx_upload;
    public Privilege stcj;
    public Privilege stcj_upload;
    public Privilege zlzp;
    public void initView(View view){

        banner = (ImageView) view.findViewById(R.id.banner);
        title_name = (TextView) view.findViewById(R.id.title_name);
        title_name.setText("质量监控");
        right_tv = (TextView) view.findViewById(R.id.right_tv);
        right_tv.setText("创建");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);
        gxys_rl = (RelativeLayout) view.findViewById(R.id.gxys_rl);
        gxys_rl.setOnClickListener(this);
        zlzp_rl = (RelativeLayout) view.findViewById(R.id.zlzp_rl);
        zlzp_rl.setOnClickListener(this);
        d_sggx_rl = (RelativeLayout) view.findViewById(R.id.d_sggx_rl);
        d_sggx_rl.setOnClickListener(this);
        stcj_rl = (RelativeLayout) view.findViewById(R.id.stcj_rl);
        stcj_rl.setOnClickListener(this);
        st_upload_rl = (RelativeLayout) view.findViewById(R.id.st_upload_rl);
        st_upload_rl.setOnClickListener(this);


        String url =  String.format(Urls.GETAdv,"SQCP_App_quality_header");

        DisplayImageOptions options = ImageUtil.getOption();
        ImageLoader.getInstance().displayImage(url, banner, options);

        DbManager dbManager =  DBUtil.getInstance(getActivity()).getDBManager();
        try{

            gjgx_select = dbManager.selector(Privilege.class).where("name","=", Constants.ToBeAcceptenceCriticalActivity).findFirst();//关键工序查询
            gjgx_confirm = dbManager.selector(Privilege.class).where("name","=", Constants.ToBeConfirmedCriticalActivity).findFirst();//关键工序确认
            gjgx_upload = dbManager.selector(Privilege.class).where("name","=", Constants.UploadCriticalActivity).findFirst();//关键工序上传
            stcj = dbManager.selector(Privilege.class).where("name","=", Constants.ProjectCollect).findFirst();//实体采集
            stcj_upload = dbManager.selector(Privilege.class).where("name","=", Constants.UploadProjectConstrution).findFirst();//实体采集上传
            zlzp = dbManager.selector(Privilege.class).where("name","=", Constants.QualitySnap).findFirst();//实体采集上传



        }catch (Exception e){


        }

        if(gjgx_select == null){
            gxys_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(gjgx_confirm == null){
            d_sggx_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(gjgx_upload == null){
            zlzp_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(stcj == null){
            stcj_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(stcj_upload == null){
            st_upload_rl.setBackgroundResource(R.drawable.round_color_disable);
        }


    }

    private void showPopupWindow(View view) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.pop_window, null);
        // 设置按钮的点击事件
        p_zlzp_tv = (TextView) contentView.findViewById(R.id.p_zlzp_tv);
        p_zlzp_tv.setOnClickListener(this);

        p_stcj_tv = (TextView) contentView.findViewById(R.id.p_stcj_tv);
        p_stcj_tv.setOnClickListener(this);

          popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.round_color_gx));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);

    }





    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.right_tv:
                showPopupWindow(v);
                break;
            case R.id.gxys_rl:
                if(gjgx_select == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(), GjgxActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.zlzp_rl:
                if(gjgx_upload == null){
                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(), ZlzpListActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.d_sggx_rl:
                if(gjgx_confirm == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(), DqrSggxActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.stcj_rl:
                if(stcj == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(), StcjActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.p_zlzp_tv:
                intent = new Intent(getActivity(), ZlzpActivity.class);
                getActivity().startActivity(intent);
                popupWindow.dismiss();
                break;
            case R.id.p_stcj_tv:

                intent = new Intent(getActivity(), StcjActivity.class);
                getActivity().startActivity(intent);
                popupWindow.dismiss();
                break;
            case R.id.st_upload_rl:

                if(stcj_upload == null){
                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(), StcjListActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }
}
